package com.example.shippxy.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.shippxy.constant.Constant;
import com.example.shippxy.utils.DateUtils;
import com.example.shippxy.utils.KeysUtils;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;

import java.text.DecimalFormat;
import java.util.*;

public class ShipxyThread implements Runnable {

    private static String token = "13b65678e1b28c6fe5ed8236ac8e4f86";
    private static String jfg = "a01ce294b485d3f88c5e0dc683d894f7";


    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                //每次获取5艘船的数据
                Iterator<Map.Entry<String, Object>> iterator = Constant.ships.entrySet().iterator();
                int index = 0;
                List<String> mmsiList = new ArrayList<>();
                while (index < 5) {
                    if (iterator.hasNext())
                        index++;
                    else
                        break;
                    Map.Entry<String, Object> next = iterator.next();
                    mmsiList.add(next.getKey());
                    //每五个mmsi休眠3秒
                    if (index >= 3 || !iterator.hasNext()) {
                        System.out.println(mmsiList);
                        mmsiList.parallelStream().forEach(mmsi -> {
                            JSONObject shipDetail = getShip(mmsi);
                            if (null != shipDetail) {
                                Constant.shipsMap.put(mmsi,shipDetail);
                            }
                        });
                        mmsiList.clear();
                        index = 0;
                        Thread.sleep(3000);
                    }
                    Thread.sleep(3000);
                    //结束后发送getships请求
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static JSONObject getShip(String mmsi) {
        JSONObject result = null;
        try {
            HttpResponse<String> response = Unirest.post("https://www.shipxy.com/ship/GetShip")
                    .header("Cookie", "token="+token+"; jfg="+jfg+"; SERVERID=6a8b1ff85dc382a410b77bedfe5653bb|1703637881|1703637848")
//                    .header("Cookie", "token=13b65678e1b28c6fe5ed8286ac8e4f86; jfg=a01ce294b485d3f88c5e0dc683d894f7;SERVERID=6a8b1ff85dc382a410b77bedfe5653bb|1703637881|1703637848")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .field("shipIDs", mmsi)
                    .field("mmsi", mmsi)
                    .asString();
            JSONObject json = JSONObject.parseObject(response.getBody());
            if(response.getStatus()!=200 || json.getInteger("status")!=0){
                jfg = KeysUtils.getNewKey(jfg);
                return null;
            }
            JSONArray array = json.getJSONArray("data");
            result = jsonHandler(array.getJSONObject(0));
            //处理json数据

        } catch (Exception e) {
            //更新 jfg
            jfg = KeysUtils.getNewKey(jfg);
        }
        return result;
    }

    private static JSONObject jsonHandler(JSONObject json){
        json.put("lon",String.format("%.6f",json.getDouble("lon")/1000000));
        json.put("lat",String.format("%.6f",json.getDouble("lat")/1000000));
        json.put("sog",String.format("%.2f",json.getDouble("sog")/100));
        json.put("cog",String.format("%.2f",json.getDouble("cog")/100));
        json.put("hdg",String.format("%.2f",json.getDouble("hdg")/100));
        json.put("laststa", DateUtils.tamp2Date(json.getLong("laststa")*1000));
        json.put("lastdyn",DateUtils.tamp2Date(json.getLong("lastdyn")*1000));
        return json;
    }
}
